/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.BanHang;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import myclass.MyAdminFrame;
import pojo.Nhanvien;

/**
 *
 * @author Cong
 */
public class QLCuaHang extends MyAdminFrame {

    /**
     * Creates new form QLCuaHang1
     */
    private static final String columns[] = {"Mã cửa hàng", "Tên cửa hàng", "Địa chỉ"};
    private DefaultTableModel tbModelcuahang;

    public QLCuaHang(Nhanvien nv) {
        super(nv);
        initComponents();

        tbModelcuahang = new DefaultTableModel();
        tbModelcuahang.setColumnIdentifiers(columns);
        jtbcuahang.setModel(tbModelcuahang);
        LoadTB();
    }

    private void LoadTB() {
        tbModelcuahang.getDataVector().clear();
        List<pojo.Cuahang> ds = dao.daoCuaHang.LaydsCuaHang();
        if (!ds.isEmpty()) {
            for (int i = 0; i < ds.size(); i++) {
                pojo.Cuahang ch = ds.get(i);
                tbModelcuahang.addRow(new Object[]{ch.getId(), ch.getTencuahang(), ch.getDiachi()});
            }
        }
    }

    private void ClearDT() {
        jtfdiachi.setText("");
        jtftencuahang.setText("");
        jlbmacuahang.setText("");
    }

    private boolean CheckField() {
        boolean kq = true;
        if ("".equals(jtftencuahang.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa nhập tên cửa hàng.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return kq;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbcuahang = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jlbmacuahang = new javax.swing.JLabel();
        jtftencuahang = new javax.swing.JTextField();
        jtfdiachi = new javax.swing.JTextField();
        jbttimkiem = new javax.swing.JButton();
        jbtreset = new javax.swing.JButton();
        jbtbaocao = new javax.swing.JButton();
        jbtthoat = new javax.swing.JButton();
        jbtluu = new javax.swing.JButton();
        jbtxoa = new javax.swing.JButton();
        jtbthem = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Quản lý cửa hàng");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Quản lý cửa hàng");

        jtbcuahang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbcuahang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbcuahangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbcuahang);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin cửa hàng"));

        jLabel2.setText("Mã cửa hàng:");

        jLabel3.setText("Tên cửa hàng:");

        jLabel7.setText("Địa chỉ:");

        jbttimkiem.setText("Tìm kiếm");
        jbttimkiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbttimkiemActionPerformed(evt);
            }
        });

        jbtreset.setText("Nhập lại");
        jbtreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtresetActionPerformed(evt);
            }
        });

        jbtbaocao.setLabel("Lập báo cáo");
        jbtbaocao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtbaocaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(22, 22, 22)
                        .addComponent(jlbmacuahang))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jbttimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jbtreset, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jbtbaocao, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtftencuahang, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                                .addComponent(jtfdiachi)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jlbmacuahang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtftencuahang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtfdiachi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbttimkiem)
                    .addComponent(jbtreset)
                    .addComponent(jbtbaocao)))
        );

        jbtthoat.setText("Thoát");
        jbtthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtthoatActionPerformed(evt);
            }
        });

        jbtluu.setText("Lưu");
        jbtluu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtluuActionPerformed(evt);
            }
        });

        jbtxoa.setText("Xóa");
        jbtxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtxoaActionPerformed(evt);
            }
        });

        jtbthem.setText("Thêm");
        jtbthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbthemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(126, 126, 126)
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jtbthem, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtxoa, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtluu, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(26, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtthoat)
                    .addComponent(jbtluu)
                    .addComponent(jbtxoa)
                    .addComponent(jtbthem))
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtbcuahangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbcuahangMouseClicked
        int dong = jtbcuahang.getSelectedRow();
        jlbmacuahang.setText(jtbcuahang.getValueAt(dong, 0).toString());
        jtftencuahang.setText(jtbcuahang.getValueAt(dong, 1).toString());
        jtfdiachi.setText(jtbcuahang.getValueAt(dong, 2).toString());
    }//GEN-LAST:event_jtbcuahangMouseClicked

    private void jbttimkiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbttimkiemActionPerformed
        pojo.Cuahang ch = new pojo.Cuahang();
        ch.setDiachi(jtfdiachi.getText());
        ch.setTencuahang(jtftencuahang.getText());
        List<pojo.Cuahang> ds = dao.daoCuaHang.FindCH(ch);
        if (!ds.isEmpty()) {
            tbModelcuahang.getDataVector().clear();
            for (int i = 0; i < ds.size(); i++) {
                pojo.Cuahang ch1 = ds.get(i);
                tbModelcuahang.addRow(new Object[]{ch1.getId(), ch1.getTencuahang(), ch1.getDiachi()});
            }
        } else {
            JOptionPane.showMessageDialog(this, "Không tìm thấy.", "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbttimkiemActionPerformed

    private void jbtresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtresetActionPerformed
        ClearDT();
    }//GEN-LAST:event_jbtresetActionPerformed

    private void jbtthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtthoatActionPerformed

    private void jbtluuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtluuActionPerformed
        if ("".equals(jlbmacuahang.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa chọn cửa hàng cần sửa.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (CheckField()) {
            pojo.Cuahang ch = new pojo.Cuahang();
            ch.setId(Long.parseLong(jlbmacuahang.getText()));
            ch.setDiachi(jtfdiachi.getText());
            ch.setTencuahang(jtftencuahang.getText());
            boolean kq = dao.daoCuaHang.LuuCH(ch);
            if (kq == true) {
                LoadTB();
            } else {
                JOptionPane.showMessageDialog(this, "Cập nhật thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jbtluuActionPerformed

    private void jbtxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtxoaActionPerformed
        if ("".equals(jlbmacuahang.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa chọn cửa hàng cần xóa.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xóa không?", "Xác nhận", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            pojo.Cuahang ch = new pojo.Cuahang();
            ch.setId(Long.parseLong(jlbmacuahang.getText()));
            boolean kq = dao.daoCuaHang.XoaCH(ch);
            if (kq == true) {
                LoadTB();
            } else {
                JOptionPane.showMessageDialog(this, "Xóa thất bại.\nXin hãy kiểm tra lại ràng buộc dữ liệu.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jbtxoaActionPerformed

    private void jtbthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbthemActionPerformed
        if (CheckField()) {
            pojo.Cuahang ch = new pojo.Cuahang();
            ch.setDiachi(jtfdiachi.getText());
            ch.setTencuahang(jtftencuahang.getText());
            boolean kq = dao.daoCuaHang.ThemCuaHang(ch);
            if (kq == true) {
                LoadTB();
            } else {
                JOptionPane.showMessageDialog(this, "Thêm thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jtbthemActionPerformed

    private void jbtbaocaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtbaocaoActionPerformed
    }//GEN-LAST:event_jbtbaocaoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtbaocao;
    private javax.swing.JButton jbtluu;
    private javax.swing.JButton jbtreset;
    private javax.swing.JButton jbtthoat;
    private javax.swing.JButton jbttimkiem;
    private javax.swing.JButton jbtxoa;
    private javax.swing.JLabel jlbmacuahang;
    private javax.swing.JTable jtbcuahang;
    private javax.swing.JButton jtbthem;
    private javax.swing.JTextField jtfdiachi;
    private javax.swing.JTextField jtftencuahang;
    // End of variables declaration//GEN-END:variables
}
